package com.example.a80205050.myapplication.Dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.a80205050.myapplication.Modle.Tb_pwd;

/**
 * Created by 80205050 on 2017/10/30.
 */

public class PwdDao {
    private DBOpenHelper helper;
    private SQLiteDatabase db;
    public PwdDao(Context context) {
        helper=new DBOpenHelper(context);
    }

    /**
     * 添加密码信息
     * @param tb_pwd
     */
    public void add(Tb_pwd tb_pwd){
        db=helper.getWritableDatabase();
        db.execSQL("insert into tb_pwd (password) values(?)",new Object[]{tb_pwd.getPassword()});
    }

    /**
     * 设置密码信息
     * @param tb_pwd
     */
    public void update(Tb_pwd tb_pwd){
        db=helper.getWritableDatabase();
        db.execSQL("update tb_pwd set password=?",new Object[]{tb_pwd.getPassword()});
    }

    /**
     * 查找密码的信息
     * @return
     */
    public Tb_pwd find(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select password from tb_pwd",null);
        if(cursor.moveToNext()){
            //将密码保存到Tb_pwd实体类中
            return new Tb_pwd(cursor.getString(cursor.getColumnIndex("password")));
        }
        //如果没有找到就返回空
        return null;
    }
    public long getCount(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select count(password) from tb_pwd",null);
        if(cursor.moveToNext()){
            return cursor.getLong(0);
        }
        return 0;
    }
}
